import * as THREE from "../../lib/three.module";
import { PLYLoader } from '../../lib/loaders/PLYLoader';

const plyLoader = function () {
  const loader = new PLYLoader();
  if (this.resourcePath) {
    loader.setResourcePath(this.resourcePath);
  }
  loader.load(
    this.modelPath,
    (geometry) => {
      geometry.computeBoundingBox();
      const box = geometry.boundingBox;
      const maxDiameter = Math.max(
        box.max.x - box.min.x,
        box.max.y - box.min.y,
        box.max.z - box.min.z
      );
      console.log(maxDiameter);
      geometry.computeVertexNormals();
      const material = new THREE.MeshStandardMaterial({
        color: 0xff5533,
        flatShading: true,
        specular: 0x111111,
        shininess: 200
      });
      const mesh = new THREE.Mesh(geometry, material);

      mesh.position.y = -0.2;
      mesh.position.z = 0.3;
      mesh.rotation.x = -Math.PI / 2;
      mesh.scale.multiplyScalar(0.001);

      mesh.castShadow = true;
      mesh.receiveShadow = true;

      this.scene.add(mesh);
      this.setScaleToFitSize(mesh);
      this.setPositionToFitGrid(mesh);
    },
    this.loadProgress.bind(this)
  );
};

export default plyLoader;
